<template>
	<view class="unt-goods-sku-contanier">
		<!-- 商品弹窗选项卡 -->
		<u-popup :show="show" :round="10" mode='bottom' @close="$emit('close')" :closeable="true">
			<view class="shop-popup-content">
				<view class="sku-list-item">
					<view class="shop-image-price flex flex-start">
						<view class="mall-image">
							<image class="mall-imageTo" :src="goodsInfo.image.url" mode="scaleToFill"></image>
						</view>
						<view class="price-choice">
							<view class="price">¥<text class="price-num">{{goodsInfo.goodsPrice}}</text></view>
							<view class="choice">选择：{{skuNamePeic}}</view>
							<view class="stockNum">库存：{{goodsInfo.stockNum}}</view>
						</view>
					</view>
					<view class="shop-specs" v-for="(item,index) in specList" :key="index">
						<view class="specs-title">{{item.specName}}</view>
						<view class="specs-content flex flex-wrap flex-start">
							<!-- activeIndex(item.specId,iv.specValueId) -->
							<view class="item" :class="{active:subIndex[item.specId]==iv.specValueId}"
								v-for="(iv,ind) in item.valueList" :key="ind"
								@click="skuClick(item.specId,iv.specValueId)">{{iv.specValue}}
							</view>
						</view>
					</view>
					<view class="shop-number flex-sb">
						<text class="number-title">数量</text>
						<u-number-box buttonSize="25" v-model="goodsNum" @change='goodsNumChange()'></u-number-box>
					</view>
				</view>
				<!-- 操作按钮 -->
				<view class="foo-item-btn" style="margin-top: 25px;">
					<view class="btn-wrapper">
						<view class="btn-item btn-item-deputy" @click="addCartGoods()" v-if="typePay==1">
							<text>加入购物车</text>
						</view>
						<view class="btn-item btn-item-deputy" @click="addCartGoods()" v-else>
							<text>立即支付</text>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import * as Cart from "@/api/cart/index.js"
	import prop from './props.js'
	export default {
		name: "unt-goods-sku",
		mixins: [prop],
		data() {
			return {
				selectArr: [], // 存放被选中的值
				subIndex: {}, // 是否选中 因为不确定是多规格还是单规格，所以这里定义数组来判断,
				goodsInfo: {},
				specList: [],
				skuList: [],
				goodsNum: 1,
			};
		},
		mounted() {
			this.initData();
		},
		methods: {
			initData() {
				const app = this;
				app.specList = app.goods.specList;
				app.skuList = app.goods.sku;
				app.goodsNum = app.goods.goodsNum == null ? 1 : app.goods.goodsNum;
				for (const item of app.specList) {
					app.subIndex = {
						...app.subIndex,
						[item.specId]: item.valueList[0].specValueId
					};
				}
				this.skuFindInfo();
			},
			skuClick(val1, val2) {
				this.subIndex = {
					...this.subIndex,
					[val1]: val2
				};
				this.skuFindInfo();
			},
			skuFindInfo() {
				const values = Object.values(this.subIndex);
				this.goodsInfo = this.skuList.find((e) => {
					return e.specValueIds == JSON.stringify(values)
				});
			},
			addCartGoods() {
				const data = {
					goodsId: this.goods.goodsId,
					goodsSkuId: this.goodsInfo.id,
					goodsNum: this.goodsNum
				};
				if (this.typePay == 2) {
					this.$unts.navTo('pages_moon/handCheckout/handCheckout', data)
				} else {
					Cart.add(data).then(res => {
						this.$unts.toast(res.message);
					}).catch()
				}
			},
			goodsNumChange(e) {
				this.goodsNum = e;
			}
		},
		computed: {
			skuNamePeic() {
				const goodsProps = this.goodsInfo.goodsProps;
				if (this.goodsInfo.goodsProps) {
					return JSON.parse(goodsProps).map((prop) => prop.value.name).join('-');
				} else {
					return '没有规格';
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.unt-goods-sku-contanier {
		.shop-popup-content {
			width: 100%;
			padding: 40rpx 30rpx;

			.shop-image-price {
				width: 100%;

				.mall-image {
					min-width: 200rpx;
					width: 240rpx;
					height: 240rpx;

					.mall-imageTo {
						width: 100%;
						height: 100%;
					}
				}

				.price-choice {
					margin-left: 20rpx;

					.price {
						color: #FC483C;

						.price-num {
							font-size: 50rpx;
							font-weight: bold;
						}
					}

					.stockNum,
					.choice {
						font-size: 28rpx;
						margin-top: 10rpx;
					}
				}

			}

			.shop-specs {
				margin: 30rpx 0px;

				.specs-title {
					font-size: 14px;
				}

				.specs-content {
					padding: 20rpx 0px;

					.item {
						font-size: 12px;
						padding: 10rpx 20rpx;
						background-color: #F2F3F7;
						border-radius: 200rpx;
						margin-right: 15rpx;
						margin-bottom: 10rpx;
					}

					.active {
						background: rgba(1, 166, 48, 0.1);
						color: #01a630;
					}
				}
			}

			.shop-number {
				font-size: 14px;
			}
		}

		// 操作按钮
		.foo-item-btn {
			flex: 1;
			padding: 0rpx 25rpx;

			.btn-wrapper {
				height: 100%;
				display: flex;
				align-items: center;
			}

			.btn-item {
				flex: 1;
				font-size: 28rpx;
				height: 72rpx;
				// margin-right: 16rpx;
				color: #fff;
				// border-radius: 50rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			// 购物车按钮
			.btn-item-deputy {
				background: linear-gradient(90deg, #63be72, #63be72);
				color: #fff;
				font-weight: bold;
				border-radius: 10rpx;
			}
		}
	}
</style>